package small.woods.msSearch.controller;

import org.springframework.web.bind.annotation.*;
import small.woods.msSearch.po.GoodsInfo;

import java.util.Date;


/**
 * @description:
 * @author: yutingting
 * @create: 2022-01-14 11:54
 **/

@RestController
@RequestMapping("/es")
public class EsController {

//    @Autowired
//    private CourseDocRepository courseDocRepository;

    @GetMapping("/save")
    public String save(){
        GoodsInfo good=new GoodsInfo();
        good.setName("测试");
        good.setId(1l);
        good.setDescription("这是第一个测试");
        good.setCreateTime(new Date());
        return "success";
    }

    @GetMapping("/getOne")
    public String get(){
        return null;
    }

//    //保存
//    @PostMapping("/add")
//    public String save(@RequestBody CourseDoc courseDoc){
//        courseDocRepository.save(courseDoc);
//        return "ok";
//    }
//
//    //删除
//    @DeleteMapping("/delete/{id}")
//    public String del(@PathVariable("id")Long id){
//        courseDocRepository.deleteById(id);
//        return "ok";
//    }
//
//
//
////----------------高级查询--------------------
//
//    @PostMapping("/search")
//    public void search(@RequestParam("queryJson") String queryJson){
//        //查询JSON字符串，对象转成查询对象
//        NativeSearchQuery nativeSearchQueryBuilder = JSON.parseObject(queryJson, NativeSearchQuery.class);
//        //执行查询
//        Page<CourseDoc> page = courseDocRepository.search(nativeSearchQueryBuilder);
//        //返回结果 ，page转成 PageList
//        System.out.println(page);
//    }

//    @PostMapping("/searchCourse")
//    public void searchCourse(@RequestBody CourseQuery courseQuery){
//        //查询JSON字符串，对象转成查询对象
//        NativeSearchQuery nativeSearchQueryBuilder = createCourseQueryForES(courseQuery).build();
//
//        //执行查询
//        Page<CourseDoc> page = courseDocRepository.search(nativeSearchQueryBuilder);
//        //返回结果 ，page转成 PageList
//        System.out.println(page);
//    }
//
//
//    //拼接查询对象courseQuery -> NativeSearchQueryBuilder
//    private NativeSearchQueryBuilder createCourseQueryForES(CourseQuery courseQuery) {
//        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//
//
//        //添加条件===============================================================================================================
//        //组合查询
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//
//        // keyword: "java"  : 查询 MathchQuery  - DSL查询
//        if(StringUtils.hasLength(courseQuery.getKeyword())){
//            boolQueryBuilder.must(QueryBuilders.matchQuery("name",courseQuery.getKeyword()));
//        }
//
//        //过滤
//        List<QueryBuilder> filter = boolQueryBuilder.filter();
//
//        //productType: ：termQuery
//        if(courseQuery.getProductType() != null){
//            filter.add(QueryBuilders.termQuery("courseTypeId",courseQuery.getProductType()));
//        }
//
//        //priceMax: null : rangeQuery
//        if(courseQuery.getPriceMax() != null){
//            filter.add(QueryBuilders.rangeQuery("price").lte(courseQuery.getPriceMax()));
//        }
//
//        //priceMin: null ：rangeQuery
//        if(courseQuery.getPriceMin() != null){
//            filter.add(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin()));
//        }
//
//        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
//
//
//        //添加分页
//        nativeSearchQueryBuilder.withPageable(PageRequest.of(courseQuery.getPage() - 1,courseQuery.getRows()));
//
//
//        //添加排序===============================================================================================================
//        // sortField:  ; sortType:==
//        if(StringUtils.hasLength(courseQuery.getSortField())){
//
//            String sortField = null;
//            switch (courseQuery.getSortField().toLowerCase()){
//                case "xl": sortField = "buyCount" ; break;
//                case "xp": sortField = "onlineDate" ; break;
//                case "jg": sortField = "price" ; break;
//                case "rq": sortField = "viewCount" ; break;
//                case "pl": sortField = "comment" ; break;
//                default:sortField = "price" ;
//            }
//
//            //排序方式
//            SortOrder sortOrder = SortOrder.DESC;
//            if(StringUtils.hasLength(courseQuery.getSortType()) && courseQuery.getSortType().toLowerCase().equals("asc")){
//                sortOrder = SortOrder.ASC;
//            }
//
//
//            nativeSearchQueryBuilder.withSort(new FieldSortBuilder(sortField).order(sortOrder));
//        }
//
//        return nativeSearchQueryBuilder;
//    }


}

